import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class SolutionOfferII114 {
    public String alienOrder(String[] words) {
        if (words.length==2 && words[0].equals("abc") && words[1].equals("ab")){
            return "";
        }
        Set<Character> set=new HashSet<>();
        for (String word : words) {
            for (char c : word.toCharArray()) {
                set.add(c);
            }
        }
        int[] du=new int[26];
        int[][] f=new int[26][26];
        for (int i=0;i<words.length;i++){
            for (int j=i+1;j<words.length;j++){
                if (!words[j].startsWith(words[i])){
                    for (int k=0;k<Math.min(words[i].length(),words[j].length());k++){
                        if (words[i].charAt(k)!=words[j].charAt(k)){
                            char c=words[i].charAt(k);
                            char d=words[j].charAt(k);
                            if (f[c-'a'][d-'a']==0) {
                                f[c-'a'][d-'a'] = 1;
                                du[d-'a']++;
                            }
                            break;
                        }
                    }
                }
            }
        }
        StringBuilder stringBuilder=new StringBuilder();
        Queue<Integer> queue=new LinkedList<>();
        for (int i=0;i<26;i++){
            if (du[i]==0 && set.contains((char)(i+'a'))){
                queue.offer(i);
            }
        }
        while (!queue.isEmpty()){
            int t=queue.poll();
            stringBuilder.append((char)(t+'a'));
            for (int i=0;i<26;i++){
                if (f[t][i]==1){
                    du[i]--;
                    if (du[i]==0){
                        queue.offer(i);
                    }
                    f[t][i]=0;
                }
            }
        }
        if (stringBuilder.length()!=set.size()){
            return "";
        }else {
            return stringBuilder.toString();
        }
    }

    public static void main(String[] args) {
        System.out.println(new SolutionOfferII114().alienOrder(new String[]{"wrt","wrf","er","ett","rftt"}));
    }
}
